package demo11.test01;

/**
 * @author hy
 * @date 2025-03-12
 **/
public class MyThread implements Runnable{
    private int bank ;
    private int money;

    public MyThread(int intBank) {
        this.bank = intBank;
    }

    public void  setMoney(int money) {
        this.money = money;
    }

    @Override
    public void run() {
        if (Thread.currentThread().getName().equals("老公")) {
            setMoney(3000);
        } else if (Thread.currentThread().getName().equals("老婆")) {
            setMoney(4000);
        }

        synchronized (this) {
            if (bank >= money) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + "取钱成功：" + money);
                bank -= money;
                System.out.println("余额为：" + bank);
            } else {
                System.out.println("取钱失败，余额不足！");
            }
        }
    }
}
